package com.wifeacountbook;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class WifeAcountBook extends Activity implements DialogInterface.OnClickListener{
	private static final String ACTION_ACOUNT_RECORD = "com.wifeacountbook.acountrecord";
	private static final String ACTION_ACOUNT_STATIC = "com.wifeacountbook.acountstatic";
	private static final String ACTION_ACOUNT_MANAGER = "com.wifeacountbook.acountmanage";
	
	
	private static final int PASSWORD_VALID_DIALOG_ID = 1;
	
	private String password = "";
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initBtnListener();
        SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        password = defaultSharedPreferences.getString(Setting.LOCAL_SAVECHECK_KEY, "");
        if(!TextUtils.isEmpty(password)){
        	showDialog(PASSWORD_VALID_DIALOG_ID);
        }
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
    	if(PASSWORD_VALID_DIALOG_ID == id){
    		View v = this.getLayoutInflater().inflate(R.layout.login, null);
    		return new AlertDialog.Builder(this).setTitle(getString(R.string.str_pwd_valid)).setView(v)
    		.setPositiveButton(getString(R.string.str_done), this)
    		.setNegativeButton(getString(R.string.str_chancel), this).create();
    	}
    	return super.onCreateDialog(id);
    }
    
    @Override
	public void onClick(DialogInterface dialog, int which) {
		if(DialogInterface.BUTTON_POSITIVE == which){
			EditText etxt = (EditText) ((AlertDialog)dialog).findViewById(R.id.password);
			String inputPwd = etxt.getText().toString();
			if(password.equals(inputPwd)){
				dialog.cancel();
			}else{
				finish();
			}
		}else if(DialogInterface.BUTTON_NEGATIVE == which){
			dialog.cancel();
			finish();
		}
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	getMenuInflater().inflate(R.menu.main_menu, menu);
    	return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	if(item.getItemId() == R.id.aboutme){
    		Intent intent = new Intent();
    		intent.setClass(this, AboutActivity.class);
    		startActivity(intent);
    	}else if(item.getItemId() == R.id.donate){
    		
    	}
    	return super.onOptionsItemSelected(item);
    }
    
	private void initBtnListener() {
		//init button callback
        Button btnRecord = (Button) findViewById(R.id.btn_record);
        Button btnStatic = (Button) findViewById(R.id.btn_static);
        Button btnManager = (Button) findViewById(R.id.btn_manager);
  
        //btnStatic.setEnabled(false);
        //btnManager.setEnabled(false);
        
        btnRecord.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ACTION_ACOUNT_RECORD);
				intent.setClass(getApplicationContext(),RecordTabActivity.class);
				startActivity(intent);
			}
		});
        btnStatic.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ACTION_ACOUNT_STATIC);
				intent.setClass(getApplicationContext(),AcountStaticer.class);
				startActivity(intent);
				
			}
		});
        
        btnManager.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ACTION_ACOUNT_MANAGER);
				intent.setClass(getApplicationContext(),Setting.class);
				startActivity(intent);
			}
		});
	}

}